const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    departmentName: '全部', //部门名称
    memberId: '',
    departmentId: '', //部门  id
    flag: 0, //0部门负责人查看 1总经理查看
    time: '', //  时间
    reportListRi: [], //日报
    reportListWeek: [], //周报
    reportList: [],
    departmentList: [],
    fieldNames: {
      text: 'name',
      value: 'code',
      children: 'items',
    },
    showCasc: false, //员工弹窗
    showTime: false, //时间弹窗
    currentDate: new Date().getTime(),
    cascaderValue: '',
    formatter(type, val) {
      if (type === 'year') {
        return val + '年';
      }
      if (type === 'month') {
        return val + '月';
      }
      if (type === 'day') {
        return val + '日';
      }
      return val;
    },
    activeNames: 1,
    myDepartmentId: ''
  },

  // 获取数据
  getData() {
    var url = app.globalData.url + "workReport/getSubordinateWorkReport",
      that = this,
      memberId = this.data.memberId,
      departmentId = this.data.myDepartmentId, //部门  id
      flag = this.data.flag, //0部门负责人查看 1总经理查看
      time = this.data.time,
      reportListRi = [],
      reportList = [],
      reportListWeek = [];

    console.log(memberId,
      departmentId,
      flag,
      time);

    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
      memberId,
      departmentId,
      flag,
      time
    }, res => {
      console.log(res);
      if (res.flag) {
        res.data.forEach(ele => {
          if (ele.workType == 0) {
            reportListRi.push(ele)
          } else {
            reportListWeek.push(ele)
          }
        })
        wx.hideLoading()
        that.setData({
          reportListRi,
          reportListWeek,
          reportList: res.data
        })
      }
    })
  },
  // 获取 员工列表
  getUserList() {
    var url = app.globalData.url + "member/getDepartment",
      that = this,
      flag = that.data.flag,
      departmentName = that.data.departmentName,
      myDepartmentId = that.data.myDepartmentId;
    console.log(this.data.myDepartmentId);
    console.log(myDepartmentId);
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {}, res => {
      console.log(res);
      if (res.flag) {
        var departmentList = res.data.departmentList;
        departmentList.forEach(ele => {
          if (flag == 0 && ele.departmentId == myDepartmentId) {
            departmentName = ele.departmentName
          }
          ele.value = ele.departmentId
          ele.text = ele.departmentName
        });
        departmentList.unshift({
          value: 0,
          text: '全部',
          departmentId: 0,
          departmentName: '全部',
        })
        console.log(departmentList);
        console.log(myDepartmentId);
        wx.hideLoading()
        that.setData({
          departmentList,
          departmentName
        })
      }
    })
  },

  // 显示弹窗
  showHandle(e) {
    console.log(e);
    var flag = e.currentTarget.dataset.flag;
    console.log(flag);
    if (flag == 'user') {
      console.log(app.globalData.member);
      if (app.globalData.member.ifManager != 1) {
        return
      }
      this.setData({
        showCasc: true
      })
    } else {
      this.setData({
        showTime: true
      })
    }
  },
  // 选择部门
  onFinish(e) {
    console.log(e);
    var obj = e.detail.selectedOptions[e.detail.selectedOptions.length - 1],
      flag = 0;
    console.log(obj)
    flag = obj.departmentId == 0 ? 1 : 0
    this.setData({
      myDepartmentId: obj.departmentId,
      departmentName: obj.departmentName,
      showCasc: false,
      flag
    })
    this.getData()
  },
  onCloseCascader() {
    this.setData({
      showCasc: false
    })
  },

  // 选择时间
  resTime(e) {
    var time = new Date(e.detail),
      formartTime = this.formatDate(time);
    console.log(time);
    console.log(formartTime);

    this.setData({
      time: formartTime,
      showTime: false
    })
    this.getData()
  },
  formatDate(date) {
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },
  // 取消选择时间 
  selectCancel() {
    this.setData({
      showTime: false
    })
  },

  // 跳转到详情页
  toDetail(e) {
    console.log(e);
    var obj = e.currentTarget.dataset.obj
    console.log(obj);
    wx.navigateTo({
      url: '/work/pages/workReport/workReport?obj=' + JSON.stringify(obj),
    })
  },
  onCollChange(e) {
    this.setData({
      activeNames: e.detail
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(app.globalData);
    var member = app.globalData.member,
      flag = '',
      myDepartmentId = app.globalData.departmentId,
      time = new Date();
    console.log(time);
    console.log(this.formatDate(time));
    console.log(member);
    console.log(myDepartmentId);
    if (member.ifManager == 1) {
      flag = 1
    } else if (member.inCharge == 1) {
      flag = 0
    }
    console.log(flag);
    this.setData({
      myDepartmentId,
      flag,
      time: this.formatDate(time),
      memberId: app.globalData.memberId
    })
    this.getUserList()
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})